<!DOCTYPE html>
<html lang="cn">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <h1>JS 创建对象</h1>
    <h1>JS 创建对象</h1>
    <h1>JS 创建对象</h1>
    <h1>JS 创建对象</h1>
    <h1>JS 创建对象</h1>
    <h1>JS 创建对象</h1>
    <hr>

    <div id="box">123567</div>

    <script>
        // Object 构造函数方式
        var obj1 = new Object();
        obj1.name = '艳艳';
        obj1.age = 17;
        obj1.getInfo = function () {
            document.write('OBJ1 <br>');
        }

        console.log(obj1);
        console.log(obj1.name);
        obj1.getInfo();

        // JSON方式  (店长推荐)
        var obj2 = {
            "age"  : "18",
            sex    : '女',
            getInfo: function (){
                document.write('OBJ2 <br>');
            }
        };
        obj2.name = '静静';

        console.log(obj2);
        obj2.getInfo();

        // 自定义构造函数方式
        function User(){
            this.name = '莉莉';
            this.age = 88;
            this.getInfo = function () {
                document.write('OBJ3 <br>');
            }
        }

        var obj3 = new User();
        console.log(obj3);
        obj3.getInfo();

        // 匿名构造函数方式
        var obj4 = new function (){
            this.name = '玥玥';
            this.age = 8;
            this.getInfo = function () {
                document.write('OBJ4 <br>');
            }
        }
        console.log(obj4);
        obj4.getInfo();

        // 测试属性
        console.log('obj1: ' + obj1.constructor); // Object
        console.log('obj2: ' + obj2.constructor); // Object
        console.log('obj3: ' + obj3.constructor); // User
        console.log('obj4: ' + obj4.constructor); // 匿名

        var h1s = document.getElementsByTagName('h1');
        console.log(h1s);
        console.log(h1s.constructor);
        console.log(h1s[0].constructor);
        var box = document.getElementById('box');
        console.log(box.constructor);
    </script>
</body>
</html>